/**
 * 
 */
package org.vsg.stock.basic.service.handler;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.vsg.stock.basic.domain.StockDayIndicator;
import org.vsg.stock.basic.persistence.file.FolderReadTickFileProcessor;
import org.vsg.stock.core.domain.StockTick;

/**
 * @author vison
 *
 */
public class OrderOverConsTradeTickHandler implements TradeTickNatureHandler {

	/**
	 * set the logger for developer code , do not use System.print(ln) to out print message.
	 */
	private static final Logger logger = LoggerFactory.getLogger(OrderOverConsTradeTickHandler.class);
	
	
	/* (non-Javadoc)
	 * @see org.vsg.stock.basic.service.handler.TradeTickNatureHandler#handle(java.util.Map, org.vsg.stock.basic.domain.StockDayIndicator, java.util.List, int, double, double)
	 */
	@Override
	public short handle(
			StockDayIndicator indicator, List<StockTick> stockTickList,
			int index, double bigOrdLevel, double midBigOrdLevel) {
		// TODO Auto-generated method stub
		StockTick st = stockTickList.get(index);
		StockTick lastSt = stockTickList.get(index-1);
		StockTick nextSt = stockTickList.get(index+1);

		boolean c1 = false;
		
		if (st.getLastCurrentPrice() 
				== lastSt.getConsignedSell1Price() || 
				st.getLastCurrentPrice() == lastSt.getConsignedSell2Price()
		) {
			c1 = true;
		}

		
		long lastTotalConBuy = lastSt.getConsignedBuy1Vol()+
			lastSt.getConsignedBuy2Vol() + lastSt.getConsignedBuy3Vol() + lastSt.getConsignedBuy4Vol()
			+ lastSt.getConsignedBuy5Vol();
		
		long totalConBuy = st.getConsignedBuy1Vol()+
		st.getConsignedBuy2Vol() + st.getConsignedBuy3Vol() + st.getConsignedBuy4Vol()
		+ st.getConsignedBuy5Vol();
		
 
		long lastTotalConSell = lastSt.getConsignedSell1Vol()+
		lastSt.getConsignedSell2Vol() + lastSt.getConsignedSell3Vol() + lastSt.getConsignedSell4Vol()
		+ lastSt.getConsignedSell5Vol();
	
		long totalConSell = st.getConsignedSell1Vol()+
		st.getConsignedSell2Vol() + st.getConsignedSell3Vol() + st.getConsignedSell4Vol()
		+ st.getConsignedSell5Vol();
		
		long diffBuy = lastTotalConBuy - totalConBuy;
		long diffSell = lastTotalConSell - totalConSell;
		
		boolean c3 = false;
		if (st.getPresentTickVol()- lastTotalConBuy >= bigOrdLevel 
			&&	st.getPresentTickVol() - totalConBuy >= bigOrdLevel
			&&	st.getPresentTickVol() - totalConSell >= bigOrdLevel
			&&	st.getPresentTickVol() - totalConSell >= bigOrdLevel
				
		) {
			c3 = true;
		}

		
		if (c1 && c3) {
			
			logger.info("stock ["+indicator.getStockCode()+"] time: " + st.getDateTime() + " , tick:" + st.getPresentTickVol() );
			
			return TradeTickNatureHandler.MATCH;
		}		
		
		return TradeTickNatureHandler.SKIP;
	}


	@Override
	public double getAmount() {
		// TODO Auto-generated method stub
		return 0;
	}


	@Override
	public long getVol() {
		// TODO Auto-generated method stub
		return 0;
	}

	
	
}
